RedOfertaDetalle = function(){
	return {
		buscar: function(){
			Ext.getCmp("redOfertaGrid").getStore().load();
		},
		confirmProcess: function(redOfertaId){
			Asg.Message.confirm(Asg.constants.CONFIRM_DELETE_MESSAGE, function(btn){
				if(btn != "no") RedOfertaDetalle.deleteRedOferta(redOfertaId);
			});
		},
		deleteRedOferta: function(redOfertaId){
			Asg.Message.progress("Actualizando Informaci\u00F3n...");
            Ext.Ajax.request({
                timeout: Asg.constants.TIMEOUT_SECONDS,
                url: Asg.constants.CONTEXT_PATH + "/red/oferta/delete",
                params: {redOfertaId: redOfertaId},
                callback: function(options, success, response){
                    Ext.Msg.hide();
                    var jsonObject = Asg.Utils.ajaxRequestGetJson(response);
                    if(!success || !jsonObject || jsonObject.failure){
                        Asg.Message.alertInvalid(Asg.constants.DEFAULT_ERROR_MESSAGE);
                        return
                    }
                    RedOfertaDetalle.buscar();
                }
            });
		},		
		initConfig: function(config){
			Ext.define("redOfertaModel", {
                extend: "Ext.data.Model",
                fields: [
	                "redOfertaId",
					"redOfertaFecha",
					"ctgEmpleado",
					"ctgOficina",
					"redEmpresaFolio",
					"redOfertaDatoPuesto",
					"redOfertaDatoSalario",
					"redOfertaDatoFormaContratacion"
	            ]
            });
			var redOfertaStore = Ext.create("Ext.data.Store", {
                //autoLoad: {params:{start: 0, limit: 50}},
                pageSize: Asg.constants.PAGE_SIZE,
                model: "redOfertaModel",
                proxy: {
	                type: "ajax",
	                url: Asg.constants.CONTEXT_PATH + "/red/oferta/findBy",
	                reader: {
	                    type: "array"/*,
	                    root: "list",
	                    totalProperty: "totalResult"*/
	                }
	            },
	            listeners: {
	                beforeload: function(store, operation, options){
	                        if(!operation) operation = {};
	                        if(!operation.params) operation.params = {};
	                        var form = Ext.getCmp("redOfertaDetalleGridForm").getForm();
	                        if(!form.isValid()){
                                Asg.Message.alertInvalidFields();
                                return false;
	                        }
	                        var values = form.getValues();
	                        for(value in values){
                                operation.params["redOfertaForm." + value] = values[value];
	                        }
	                        if(operation.params.restart){
                                operation.start = 0;
                                operation.page = 1;
	                        }
	                }
	            },
	            groupers: [{property: "asgCliente"}]
	        });
			return {
				items:[
			       {
			        	xtype: "form",
			        	id: "redOfertaDetalleGridForm",
			        	height: 105,
			        	bodyStyle: "background: #DFE8F6",
			        	layout: "fit",
			        	border: false,
						items: {
							xtype: "fieldset",
							title: "PARAMETROS DE BUSQUEDA",
							style: "background: #FFFFFF;",
							layout: {
	        	        		type: "table",
								columns: 6,
								tableAttrs: {
						            style: {width: "740px"},
						            align: "center"
								}
	        	        	},
	        	        	defaults: Asg.Form.defaults({width: 240, labelAlign: "top", colspan: 2}),
	        	        	items: [
	    	        	        {
	    	        	        	xtype: "radio",
	    	        	        	name: "redOfertaOrigen",
	    	        	        	boxLabel: "Mis Ofertas",
	    	        	        	inputValue: "1",
	    	        	        	checked: true
	    	        	        },{
	    	        	        	xtype: "radio",
	    	        	        	name: "redOfertaOrigen",
	    	        	        	boxLabel: "Ofertas de Mi Oficina",
	    	        	        	inputValue: "2"
	    	        	        },{
	    	        	        	xtype: "radio",
	    	        	        	name: "redOfertaOrigen",
	    	        	        	boxLabel: "Todas las Ofertas",
	    	        	        	inputValue: "3"
	    	        	        },{
	    	        	        	xtype: "radio",
	    	        	        	name: "redOfertaEstado",
	    	        	        	boxLabel: "Activas",
	    	        	        	inputValue: "1",
	    	        	        	checked: true
	    	        	        },{
	    	        	        	xtype: "radio",
	    	        	        	name: "redOfertaEstado",
	    	        	        	boxLabel: "Inactivas",
	    	        	        	inputValue: "2"
	    	        	        },{
	    	        	        	xtype: "radio",
	    	        	        	name: "redOfertaEstado",
	    	        	        	boxLabel: "Todas",
	    	        	        	inputValue: "3"
	    	        	        }
		        	        ]
						}
			        },{
			        	xtype: "grid",
			        	id: "redOfertaGrid",
			        	flex: 1,
			        	frame: true,
			        	store: redOfertaStore,
                        columns: [
                           {
                        	   xtype: "actioncolumn",
                        	   width: 60,
	        	        	   hidden: false,
	        	        	   items: [
        	        	           {
        	        	        	   icon: Asg.constants.CONTEXT_PATH + "/resources/images/open.png", 
        	        	        	   iconCls: "icon", 
        	        	        	   handler: function(grid, rowIndex, colIndex) {
        	        	        		   var rows = [grid.getStore().getAt(rowIndex)];
        	        	        		   RedOferta.openWindow(rows[0].get("redOfertaId"));
        	        	        	   }
        	        	           },{
        	        	        	   icon: Asg.constants.CONTEXT_PATH + "/resources/images/delete.png", 
        	        	        	   iconCls: "icon", 
        	        	        	   handler: function(grid, rowIndex, colIndex) {
        	        	        		   var rows = [grid.getStore().getAt(rowIndex)];
        	        	        		   RedOfertaDetalle.confirmProcess(rows[0].get("redOfertaId"));
        	        	        	   }
        	        	           }
    	        	           ]
                           },
		                   {header: "Folio", dataIndex: "redOfertaId", width: 100},
		                   {header: "Fecha Registro", dataIndex: "redOfertaFecha", width: 90},
		                   {header: "Gestor", dataIndex: "ctgEmpleado", flex: 1, minWidth: 150},
		                   {header: "Oficina", dataIndex: "ctgOficina", flex: 1, minWidth: 150},
		                   {header: "Empresa", dataIndex: "redEmpresaFolio", width: 100},
		                   {header: "Puesto", dataIndex: "redOfertaDatoPuesto", width: 100},
		                   {header: "Contrataci\u00F3n", dataIndex: "redOfertaDatoFormaContratacion", width: 100},
		                   {header: "Salario", dataIndex: "redOfertaDatoSalario", width: 70, renderer: "webMoney"}
	                    ],
			        	dockedItems: [
                           {
                        	   xtype: "toolbar",
                        	   dock: "top",
                        	   items: [
                        	          "->",
                    	           {
                    	        	   text: "Buscar",
                    	        	   iconCls: Asg.constants.SEARCH_ICON,
                    	        	   handler: RedOfertaDetalle.buscar
                    	           },
                    	           "-",
                    	           {
                    	        	   text: "Agregar",
                    	        	   iconCls: Asg.constants.ADD_ICON,
                    	        	   handler: function(){
                    	        		   RedOferta.openWindow();
                    	        		   //RedDiscapacidad.showWindow();
                    	        	   }
                    	           }
                	           ]
                           }
                       ]
					}
		       ]
			};
		}
	};
}();